//vue 框架

var app2 = new Vue({
    el: "#total",
    data: {
        money: 1,
        change: false,
        btn: false
    },
    methods: {
        checkOut: function() {
            if (app.shop_car.length == 0) {
                show("没有商品,快去添加商品到购物车吧")
            } else {
                instance({
                    method: "get",
                    url: "api/shopping/checkOut"
                }).then(res => {
                    console.log(res)
                    show("结算中...")
                    const div = document.createElement('divform');
                    div.innerHTML = res.data.data;
                    document.body.appendChild(div);
                    document.forms[0].acceptCharset = 'utf-8'; //保持与支付宝默认编码格式一致，如果不一致将会出现：调试错误，请回到请求来源地，重新发起请求，错误代码 invalid-signature 错误原因: 验签出错，建议检查签名字符串或签名私钥与应用公钥是否匹配
                    document.forms[0].submit();
                })
            }
        }
    },
    watch: {
        money: function(val, oldVal) { //同步goods_num到数据库
            if (this.change) {
                //当不断输入字符时，因为延时执行还没有开始，就被清除，然后重新生成新的延时器
                //虽然不停的清除，生成新的延时器，但在输入的时候延时器内部的方法都一直没有被执行
                this.btn = true
                clearTimeout(this.timer); //清除延迟执行
                this.timer = setTimeout(() => { //设置延迟执行
                    updateCart() //上传购物车数据
                }, 1000);

            }
        }
    },

})

//上传
function updateCart() {
    for (var i = 0; i < app.goods_num.length; ++i) {
        if (app.goods_num[i] != app.shop_car[i].goods_number) {
            instance({
                method: 'get',
                url: "api/shopping/insert?goods_id=" + app.shop_car[i].goods_id + "&goods_number=" + app.shop_car[i].goods_number
            }).then(res => {
                //跟新成功 放开btn
                show("更新成功")
                app2.btn = false
            })
            app.goods_num[i] = app.shop_car[i].goods_number //设置相同避免下次跳转
        }
    }
}





var app = new Vue({
    el: "#app",
    data: {
        shop_car: [],
        goods_num: []
    },
    methods: {
        getData: function() { //获取购物车信息

            instance({
                method: 'get',
                url: 'http://47.119.134.71:8080/api/shopping/findAll',

            }).then(res => {
                if (res.data.info.code == 101) { //根据返回值进行判断 是否登录
                    //如果登录 获取数据
                    this.shop_car = res.data.data
                    this.subMoney()
                    sleep(1000).then(() => {
                        app2.change = true
                    })

                } else if (res.data.info.code == 201) {
                    show("未登录")
                    sleep(1000).then(() => {
                        window.location = "login.html"
                    })
                }


            }).catch(err => {
                console.log(err);
            })

        },


        reduce: function(index) { //购物车商品减少

            if (this.shop_car[index].goods_number > 1) {
                this.shop_car[index].goods_number--;
                this.addMoney(-this.shop_car[index].goods_price)
            } else
                window.alert("不能再少了!")
        },

        add: function(index) { //购物车商品增加

            if (this.shop_car[index].goods_number < this.shop_car[index].goods_number * 2) {

                this.shop_car[index].goods_number++;
                this.addMoney(this.shop_car[index].goods_price)
            } else
                window.alert("不能再多了!")

        },


        subMoney: function() { //计算初始总价
            for (var i = 0; i < this.shop_car.length; ++i) {
                app.goods_num[i] = this.shop_car[i].goods_number
                app2.money = app2.money + this.shop_car[i].goods_price * this.shop_car[i].goods_number
            }

        },

        addMoney: function(m) { //增加价格
            app2.money += m
        }
    },


    mounted: function() { //初始化渲染

        this.getData()


    }
})